package com.taoge.electricalAppSystem.utils;

import com.yan.domain.PageInfo;
import com.yan.utils.JDBCUtil;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * @author taoge
 * @Date 2024/8/23 14:18
 * @Description
 */
public final class BaseDAO {

    // 添加一条
    public static int insertOneRow(String sql , Object... params){
        Connection connection = null;
        PreparedStatement ps = null;
        try {

            if(sql == null || "".equals(sql) || sql.length() == 0){
                throw new IllegalArgumentException("参数不合法");
            }

            connection = JDBCUtil.connection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1 , params[i]);
            }

            int count = ps.executeUpdate();
            return count;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.Close(ps,connection);
        }
        return 0;
    }

    // 删除一条
    public static int deleteOneRow(String sql , Object... params){
        Connection connection = null;
        PreparedStatement ps = null;
        try {

            if(sql == null || "".equals(sql) || sql.length() == 0){
                throw new IllegalArgumentException("参数不合法");
            }

            connection = JDBCUtil.connection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1 , params[i]);
            }

            int count = ps.executeUpdate();
            return count;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.Close(ps,connection);
        }
        return 0;
    }

    // 删除多条
    public static int deleteMutiplyRow(String sql , Integer... ids) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            if(sql == null || "".equals(sql) || sql.length() == 0){
                throw new IllegalArgumentException("参数不合法");
            }
            connection = JDBCUtil.connection();
            ps = connection.prepareStatement(sql);
            connection.setAutoCommit(false);
            for (Integer id : ids) {
                ps.setObject(1 , id);
                ps.addBatch();
            }
            int[] result = ps.executeBatch();
            int count = 0;
            for (int i = 0; i < result.length; i++) {
                if(result[i] == 1){
                    count++;
                }else{
                    throw new SQLException("有一条或多条数据删除错误");
                }
            }
            connection.commit();
            return count;
        }  catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        } finally {
            JDBCUtil.Close(ps,connection);
        }
        return 0;
    }

    // 更新
    public static int updateOneRow(String sql , Object... params){
        Connection connection = null;
        PreparedStatement ps = null;
        try {

            if(sql == null || "".equals(sql) || sql.length() == 0){
                throw new IllegalArgumentException("参数不合法");
            }

            connection = JDBCUtil.connection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1 , params[i]);
            }

            int count = ps.executeUpdate();
            return count;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.Close(ps,connection);
        }
        return 0;
    }

    // 查询所有或条件查询
    public static <T> List<T> selectMutiply(String sql , Class<T> c, Object... params)  {

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            if(sql == null || "".equals(sql) || sql.length() == 0 || c == null){
                throw new IllegalArgumentException("参数不合法");
            }
            // 创建连接
            connection = JDBCUtil.connection();
            // 创建传输对象
            ps = connection.prepareStatement(sql);
            // 替换问号站位符
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1 , params[i]);
            }
            rs = ps.executeQuery();
            // 获取列名
            ResultSetMetaData metaData = rs.getMetaData();
            // 获取列名个数
            int columnCount = metaData.getColumnCount();
            // 创建数组
            LinkedList<T> list = new LinkedList<>();
            // 遍历每一行
            while(rs.next()){
                // 通过反射创建对象
                Constructor<T> constructor = c.getConstructor();
                T t = constructor.newInstance();
                // 遍历列名
                for (int i = 1; i <= columnCount; i++) {
                    // 获取列名和数据
                    String columnName = metaData.getColumnName(i);
                    Object value = rs.getObject(i);
                    // 封装
                    Field declaredField = c.getDeclaredField(columnName);
                    declaredField.setAccessible(true);
                    declaredField.set(t , value);
                }
                list.add(t);
            }
            return list;
        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.Close(rs,ps,connection);
        }
        return null;
    }

    // 查询数据库中表数据个数
    public static int inspectDate(String sql , Object... params){
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            connection = JDBCUtil.connection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1 , params[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()){
                 int count =  rs.getInt(1);
                return count;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.Close(rs,ps,connection);
        }
        return 0;
    }
    // 分页查询
    public static <T> PageInfo<T> pagingSelect(String sql , Class<T> c , Integer pageNum , Integer pageSize , Object... parmars) throws SQLException {
        // 检验参数
        if(sql == null || "".equals(sql) || sql.length() == 0 || c == null){
            throw new IllegalArgumentException("参数不合法");
        }
        // 拼接sql
        sql = sql + " limit " + (pageNum-1)*pageSize + "," + pageSize;
        // 调用方法放回list
        List<T> list = BaseDAO.selectMutiply(sql, c, parmars);
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setDate(list);
        pageInfo.setCountNum(((long) list.size()));
        return pageInfo;
    }
}
